﻿using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Security.Policy;
using System.Threading.Tasks;
using WaterCloud.Code;
using WaterCloud.Domain;
using WaterCloud.Domain.Entity.STZBManage;
using WaterCloud.Domain.Statistics;
using WaterCloud.Service.STZBManage;

namespace WaterCloud.Web.Controllers.Api
{
    /// <summary>
    /// 率土之滨登入注册接口
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    [LoginFilter] //验证是否登录
    public class STZBController : ControllerBase
    {
        public STZBService _stzbService { get; set; }

        public STZB_CbgUrlService _cbgUrlService { get; set; }

        public WeaponsService _weaponsService { get; set; }

        public SsqhService _ssqhService { get; set; }

        /// <summary>
        /// 注册账号
        /// </summary>
        /// <param name="name">账号</param>
        /// <param name="pwd">密码</param>
        /// <returns></returns>
        [HttpPost]
        [AllowAnonymous] //不验证登录
        public async Task<AlwaysResult> EnrollAccount(string name, string pwd)
        {
            int state = await _stzbService.EnrollAccount(name, pwd);
            if (state == -1) return new AlwaysResult { state = ResultType.error.ToString(), message = "账号已存在" };
            return new AlwaysResult { state = ResultType.success.ToString(), message = "注册成功" };
        }

        /// <summary>
        /// 登录接口
        /// </summary>
        /// <param name="name">账号</param>
        /// <param name="pwd">密码</param>
        /// <returns></returns>
        [HttpPost]
        [AllowAnonymous] //不验证登录
        public async Task<AlwaysResult> Loggin_Account(string name, string pwd)
        {
            //查询当前账号密码是否存在
            var user = await _stzbService.Loggin_Account(name, pwd);
            if (user == null) return new AlwaysResult { state = ResultType.error.ToString(), message = "当前账号不存在" };
            if (user.State == 0) //待审核
            {
                return new AlwaysResult { state = ResultType.error.ToString(), message = "当前账号待审核" };
            }
            else if (user.State == 2) //审核拒绝
            {
                return new AlwaysResult { state = ResultType.error.ToString(), message = "当前账号审核被拒绝" };
            }
            //获取token
            var apitoken = Utils.GuId();
            if (!string.IsNullOrEmpty(OperatorProvider.Provider.GetToken()))
            {
                apitoken = OperatorProvider.Provider.GetToken();
            }
            return new AlwaysResult<string> { state = ResultType.success.ToString(), message = "登录成功。", data = apitoken };
        }

        /// <summary>
        /// 添加藏宝阁链接
        /// </summary>
        /// <param name="urlStr">链接字符串多个,号隔开</param>
        /// <returns></returns>
        [HttpPost]
        [AllowAnonymous] //不验证登录
        public async Task<AlwaysResult> AddCbgUrl(string urlStr)
        {
            try
            {
                await _cbgUrlService.Add_CbgUrl(urlStr);
                return new AlwaysResult { state = ResultType.success.ToString(), message = "操作成功" };
            }
            catch (Exception ex)
            {
                return new AlwaysResult { state = ResultType.error.ToString(), message = "操作失败" };
            }
        }

        /// <summary>
        /// 修改点击状态和爬取时间
        /// </summary>
        /// <param name="url">链接数组</param>
        /// <returns></returns>
        [HttpPost]
        [AllowAnonymous] //不验证登录
        public async Task<AlwaysResult> Update_CbgUrl([FromBody] string[] url)
        {
            try
            {
                await _cbgUrlService.Update_CbgUrl(url);
                return new AlwaysResult { state = ResultType.success.ToString(), message = "操作成功" };
            }
            catch (Exception ex)
            {
                return new AlwaysResult { state = ResultType.error.ToString(), message = "操作失败" };
            }
        }

        /// <summary>
        /// 获取今日处理的藏宝阁链接
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [AllowAnonymous] //不验证登录
        public async Task<SqlSugarDataResult<CbgUrl>> GetCbgUrl()
        {
            return await _cbgUrlService.GetCbgUrl();
        }

        /// <summary>
        /// 添加武器数据
        /// </summary>
        /// <param name="weapons">数据信息</param>
        /// <returns></returns>
        [HttpPost]
        [AllowAnonymous] //不验证登录
        public async Task<AlwaysResult> AddWeapons(Weapons weapons)
        {
            try
            {
                await _weaponsService.AddWeapons(weapons);
                return new AlwaysResult { state = ResultType.success.ToString(), message = "操作成功" };
            }
            catch (Exception ex)
            {
                return new AlwaysResult { state = ResultType.error.ToString(), message = "操作失败" };
            }
        }

        /// <summary>
        /// 获取武器数据
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [AllowAnonymous] //不验证登录
        public async Task<AlwaysResult> WeaponsTable()
        {
            try
            {
                var data = await _weaponsService.WeaponsTable();
                return new AlwaysResult<List<Weapons>> { state = ResultType.success.ToString(), message = "操作成功", data = data, count = data.Count };
            }
            catch (Exception ex)
            {
                return new AlwaysResult { state = ResultType.error.ToString(), message = $"操作失败：{ex.Message}" };
            }
        }

        /// <summary>
        /// 添加试师区号
        /// </summary>
        /// <param name="code">区号</param>
        /// <returns></returns>
        [HttpPost]
        [AllowAnonymous] //不验证登录
        public async Task<AlwaysResult> AddSsqh(string code)
        {
            try
            {
                await _ssqhService.AddSsqh(code);
                return new AlwaysResult { state = ResultType.success.ToString(), message = "操作成功" };
            }
            catch (Exception ex)
            {
                return new AlwaysResult { state = ResultType.error.ToString(), message = "操作失败" };
            }
        }

        /// <summary>
        /// 获取今日爬取数量、总待爬取数量
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [AllowAnonymous]
        public async Task<AlwaysResult> GetDayCrawlNum()
        {
            try
            {
                var data = await _cbgUrlService.GetDayCrawlNum();
                return new AlwaysResult<CrawlNum> { state = ResultType.success.ToString(), message = "操作成功", data = data };
            }
            catch (Exception)
            {
                return new AlwaysResult { state = ResultType.error.ToString(), message = "操作失败" };
            }
        }
    }
}
